package pl.jsi.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import pl.jsi.Student;

public class JpaStudentDao extends JpaDaoSupport implements StudentDao {
	
	
	
	/* (non-Javadoc)
	 * @see pl.jsi.dao.StudentDao#get(java.lang.Integer)
	 */
	public Student get(Integer id){
		return (Student) getJpaTemplate().find(Student.class, id);
	}
	
	/* (non-Javadoc)
	 * @see pl.jsi.dao.StudentDao#saveOrUpdate(pl.jsi.Student)
	 */
	public void saveOrUpdate(Student student){
		getJpaTemplate().persist(student);
	}
	
	public Student merge(Student student){
		return (Student) getJpaTemplate().merge(student);
	}
	
	/* (non-Javadoc)
	 * @see pl.jsi.dao.StudentDao#delete(pl.jsi.Student)
	 */
	public void delete(Student student){
		getJpaTemplate().remove(student);
	}
	
	/* (non-Javadoc)
	 * @see pl.jsi.dao.StudentDao#getList()
	 */
	@SuppressWarnings({ "unchecked", "unchecked" })
	public List<Student> getList(){
		// return getJpaTemplate().find("from Student");
		return getJpaTemplate().executeFind(new JpaCallback(){
			public Object doInJpa(final EntityManager em)
					throws PersistenceException {
				
				return em.createQuery("from Student").getResultList();
			}
			
		});
		
	}
	
	
	
}
